package com.xique.park.bean.transform.client;

import cn.hutool.core.util.StrUtil;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkIn;
import com.xique.park.bean.domain.ParkOut;
import com.xique.park.bean.domain.PayOrder;
import com.xique.park.bean.vo.client.ClientParkInVo;
import com.xique.park.bean.vo.client.ClientParkOutVo;
import com.xique.park.service.service.IPayOrderService;
import com.xique.system.api.RemoteSystemDictService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: caogq
 * @Date: 2021/7/26 11:57
 * @Description:
 */
@Component
public class ClientHomePageTransfer {

    @Autowired
    private AliossUtil aliossUtil;

    @Autowired
    private RemoteSystemDictService remoteSystemDictService;

    @Autowired
    private IPayOrderService payOrderService;

    public ClientParkInVo parkInTransferClientParkInVo(ParkIn parkIn) {
        ClientParkInVo clientParkInVo = new ClientParkInVo();
        clientParkInVo.setInCarPlate(parkIn.getInCarPlate());
        clientParkInVo.setInRemark(parkIn.getInRemark());
        clientParkInVo.setMngType(parkIn.getMngType());
        // 入场状态
        String inStatus = parkIn.getStatus();
        String inStatusLabel = remoteSystemDictService.getDictLabel("car_pass_status", inStatus).getData();
        clientParkInVo.setStatus(inStatusLabel);
        if (parkIn.getInTime() != null) {
            clientParkInVo.setInTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, parkIn.getInTime()));
        }
        clientParkInVo.setInId(String.valueOf(parkIn.getId()));
        if (null != parkIn.getInLaneId()) {
            clientParkInVo.setLaneId(parkIn.getInLaneId().toString());
            clientParkInVo.setPkSerial(parkIn.getParkingSerial());
            clientParkInVo.setPassWay(parkIn.getInType());
        }
        if (StringUtils.isNotEmpty(parkIn.getInPic())) {
            clientParkInVo.setInCarPic(aliossUtil.getJzyGetObjectUrl(parkIn.getInPic()).toString());
        }
        return clientParkInVo;
    }

    public ClientParkOutVo parkOutTransferClientParkOutVo(ParkOut parkOut) {
        ClientParkOutVo clientParkOutVo = new ClientParkOutVo();
        clientParkOutVo.setOutCarPlate(parkOut.getOutCarPlate());
        clientParkOutVo.setMngType(parkOut.getMngType());
        clientParkOutVo.setOutRemark(parkOut.getOutRemark());

        // 出场状态
        String outStatus = parkOut.getStatus();
        String outStatusLabel = remoteSystemDictService.getDictLabel("car_pass_status", outStatus).getData();
        clientParkOutVo.setStatus(outStatusLabel);

        // 出场时间
        if (parkOut.getOutTime() != null) {
            clientParkOutVo.setOutTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, parkOut.getOutTime()));
        }

        clientParkOutVo.setOutId(String.valueOf(parkOut.getId()));

        if (null != parkOut.getOutLaneId()) {
            clientParkOutVo.setLaneId(parkOut.getOutLaneId().toString());
            clientParkOutVo.setPkSerial(parkOut.getParkingSerial());
            clientParkOutVo.setPassWay(parkOut.getOutType());
        }

        if (StringUtils.isNotEmpty(parkOut.getOutPic())) {
            clientParkOutVo.setOutCarPic(aliossUtil.getJzyGetObjectUrl(parkOut.getOutPic()).toString());
        }

        if (parkOut.getActualFee() != null && parkOut.getActualFee() > 0) {
            Double fee = parkOut.getActualFee().doubleValue() / 100;
            clientParkOutVo.setFee(String.valueOf(fee));
        } else {
            if (parkOut.getMayFee() != null && parkOut.getMayFee() > 0) {
                Double fee = parkOut.getMayFee().doubleValue() / 100;
                clientParkOutVo.setFee(String.valueOf(fee));
            } else {
                clientParkOutVo.setFee("0");
            }
        }
        // 增加支付类型的处理，如果是收费放行，查询支付方式
        if (StringUtils.isNotEmpty(parkOut.getOutType()) && ("5".equals(parkOut.getOutType()) || "1".equals(parkOut.getOutType()))) {
            PayOrder payOrder = new PayOrder();
            payOrder.setParkId(parkOut.getParkId());
            payOrder.setPlate(parkOut.getOutCarPlate());
            payOrder.setParkingSerial(parkOut.getParkingSerial());
            List<PayOrder> payOrderList = payOrderService.selectPayOrderList(payOrder);
            if (CollectionUtils.isNotEmpty(payOrderList)) {
                String payType = payOrderList.get(0).getPayType();
                if (StrUtil.isNotBlank(payType)) {
                    String payTypeLabel = remoteSystemDictService.getDictLabel("pay_type", payType).getData();
                    clientParkOutVo.setPayType(payTypeLabel);
                    clientParkOutVo.setOutRemark(payTypeLabel + "支付放行");
                }
            }
        }
        return clientParkOutVo;
    }
}
